package com.zhaoc.web.action;

import com.zhaoc.domain.Area;
import com.zhaoc.service.IAreaService;
import com.zhaoc.utils.PinYin4jUtils;
import com.zhaoc.web.base.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>title:com.zhaoc.web.action</p>
 * date:2018/8/2 0002
 * time:23:51
 * author:赵成
 * user:13566
 */
@ParentPackage("json-default")
public class AreaAction extends BaseAction<Area> {

    private Area area = new Area();

    @Override
    public Area getModel() {
        return area;
    }

    //注入业务层对象
    @Autowired
    private IAreaService areaService;

    //接收上传文件
    private File file;

    public void setFile(File file) {
        this.file = file;
    }

    //批量区域数据导入
    @Action(value = "area_batchImport")
    public void batchImport() throws IOException{
        List<Area> list = new ArrayList<Area>();
        //编写解析代码逻辑
        //基于.xls格式解析hssf
        //1.加载EXCEL文件对象
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
        //2.读取一个sheet
        HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
        //3.读取sheet中每一行
        for (Row row : sheet) {
            if (row.getRowNum()==0){
                //第一行数据,跳过
                continue;
            }
            if(row.getCell(0) == null
                    || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
                //空行数据,跳过
                continue;
            }
            Area area = new Area();
            area.setId(row.getCell(0).getStringCellValue());
            area.setProvince(row.getCell(1).getStringCellValue());
            area.setCity(row.getCell(2).getStringCellValue());
            area.setDistrict(row.getCell(3).getStringCellValue());
            area.setPostcode(row.getCell(4).getStringCellValue());

            //基于pinyin4j生成城市编码和简码
            String province = area.getProvince();
            String city = area.getCity();
            String district = area.getDistrict();
            province = province.substring(0, province.length() - 1);
            city = city.substring(0, city.length() - 1);
            district = district.substring(0, district.length() - 1);
            // 简码
            String[] headArray = PinYin4jUtils.getHeadByString(province + city
                    + district);
            StringBuffer buffer = new StringBuffer();
            for (String headStr : headArray) {
                buffer.append(headStr);
            }
            String shortcode = buffer.toString();
            area.setShortcode(shortcode);
            // 城市编码
            String citycode = PinYin4jUtils.hanziToPinyin(city, "");
            area.setCitycode(citycode);


            list.add(area);
        }
        //调用业务层
        areaService.saveBatch(list);
    }



    //区域分页查询
    @Action(value = "area_pageQuery",results = {@Result(name = "success",type = "json")})
    public String pageQuery(){
        //构造分页查询对象
        Pageable pageable = new PageRequest(page-1,rows);
        //构造条件查询对象
        Specification<Area> specification = new Specification<Area>() {
            @Override
            public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //创建保存条件的集合对象
                List<Predicate> list = new ArrayList<Predicate>();

                //添加条件
                if (StringUtils.isNotBlank(area.getProvince())){
                    //根据省份查询,模糊
                    Predicate p1 = cb.like(root.<String>get("province").as(String.class),
                            "%" + area.getProvince() + "%");
                    list.add(p1);
                }
                if (StringUtils.isNotBlank(area.getCity())){
                    //根据城市模糊查询
                    Predicate p2 = cb.like(root.<String>get("city").as(String.class),
                            "%" + area.getCity() + "%");
                    list.add(p2);
                }
                if (StringUtils.isNotBlank(area.getDistrict())){
                    //根据区域模糊查询
                    Predicate p3 = cb.like(root.<String>get("district").as(String.class),
                            "%" + area.getDistrict() + "%");
                    list.add(p3);
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        //调用业务层完成查询
        Page<Area> pageData = areaService.findPageData(specification,pageable);

        //将结果集压入栈顶
        pushPageDataToValueStack(pageData);

        return SUCCESS;
    }


}
